package com.zhihuishu.micro.course.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.zhihuishu.micro.course.dao.CourseExtendPropMapper;
import com.zhihuishu.micro.course.dto.CourseExtendDto;
import com.zhihuishu.micro.course.helper.StringHelper;
import com.zhihuishu.micro.course.helper.data.KVPair;
import com.zhihuishu.micro.course.service.CourseExtendService;
import com.zhihuishu.toolkit.helper.BeanHelper;

@Service("courseExtendService")
public class CourseExtendServiceImpl extends BaseService implements CourseExtendService {

	@Resource
	private CourseExtendPropMapper courseExtendPropMapper ;
	
	@Override
	public CourseExtendDto get(long courseId) {
		Map<String ,String> map = toMap(courseExtendPropMapper.list(courseId)) ;
		return BeanHelper.transTo(map ,CourseExtendDto.class) ;
	}

	/**
	 * 结果集转换为Map对象
	 * @param data
	 * @return
	 */
	private Map<String ,String> toMap(List<Map<String ,String>> data) {
		if(CollectionUtils.isEmpty(data)) return null ;
		Map<String ,String> target = new HashMap<String, String>() ;
		for(Map<String ,String> map : data) {
			String key = null ;
			String value = null ;
			for(Entry<String ,String> entry : map.entrySet()) {
				if(entry.getKey().equals("PROP_NAME")) {
					key = entry.getValue() ;
				}
				if(entry.getKey().equals("PROP_VALUE")) {
					value = entry.getValue() ;
				}
			}
			// 将Key转换为驼峰命名，便于与属性匹配
			if(key != null && value != null) {
				target.put(StringHelper.underscoreToCamel(key), value) ;
			}
		}		
		return target ;
	}

	@Override
	public void update(long courseId, CourseExtendDto ext) {
		Assert.notNull(ext);
		// 转换为Map
		Map<String ,String> map = BeanHelper.describe(ext) ;
		Set<String> propNames = new HashSet<String>() ;	// 属性名称集合
		for(Map.Entry<String, String> entry : map.entrySet()) {
			if(entry.getValue() == null) continue ;
			String key = String.valueOf(entry.getKey()) ;
			if(StringUtils.equals(key, "courseId")) continue ;
			// 填充属性名称集合
			propNames.add(StringHelper.camelToUnderscore(key)) ;
		}
		
		// 根据属性名称集合查询未写入数据库部分属性(已存在更新、不存在则创建)
		Set<KVPair<String ,String>> updateDatas = new HashSet<KVPair<String,String>>() ;
		Set<KVPair<String ,String>> saveDatas = new HashSet<KVPair<String,String>>() ;
		Set<String> existsPropNames = courseExtendPropMapper.contains(courseId, propNames) ;
		for(Map.Entry<String, String> entry : map.entrySet()) {
			if(entry.getValue() == null) continue ;
			String key = String.valueOf(entry.getKey()) ;
			if(StringUtils.equals(key, "courseId")) continue ;
			String value = String.valueOf(entry.getValue()) ;
			// 已存在，更新
			if(!CollectionUtils.isEmpty(existsPropNames) && existsPropNames.contains(StringHelper.camelToUnderscore(key))) {
				// 填充要更新键值对
				updateDatas.add(new KVPair<String, String>(StringHelper.camelToUnderscore(key), value)) ; 
			} else {
				saveDatas.add(new KVPair<String, String>(StringHelper.camelToUnderscore(key), value)) ; 
			}
		}		
		
		// 写入数据库
		if(!CollectionUtils.isEmpty(saveDatas)) {
			courseExtendPropMapper.save(courseId, saveDatas);
		}
		
		// 更新数据库
		if(!CollectionUtils.isEmpty(updateDatas)) {
			// 循环更新，取代批量UPDATE语句(变形虫不支持)
			for(KVPair<String ,String> kv : updateDatas) {
				courseExtendPropMapper.update(courseId, kv.getKey() ,kv.getValue());
			}
		}
	}

	@Override
	public void save(long courseId, CourseExtendDto ext) {
		// 转换为Map
		Map<String ,String> map = BeanHelper.describe(ext) ;
		Set<KVPair<String ,String>> datas = new HashSet<KVPair<String,String>>() ;
		for(Map.Entry<String, String> entry : map.entrySet()) {
			if(entry.getValue() == null) continue ;
			datas.add(new KVPair<String, String>(StringHelper.camelToUnderscore(entry.getKey()), entry.getValue())) ; 
		}
		// 更新数据库
		if(!CollectionUtils.isEmpty(datas)) {
			courseExtendPropMapper.save(courseId, datas);
		}
	}

	@Override
	public List<Map<String, Object>> getUpdateTimeByCourseIdsAndKey(List<Long> courseIds, String key) {
		return courseExtendPropMapper.getUpdateTimeByCourseIdsAndKey(courseIds, key);
	}

  

}
